package com.wisely.highlight_spring4.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @ClassName AwareService
 * @Package com.wisely.highlight_spring4.ch3.aware
 * @Description TODO
 * @Author zjb
 * @Date 2018/7/20 17:05
 * @Version 1.0
 **/
@Service
//实现BeanNameAware、ResourceLoaderAware接口，获得Bean名称和资源加载的服务
public class AwareService implements BeanNameAware,ResourceLoaderAware {
    private  String beanName;
    private ResourceLoader loader;

    //实现ResourceLoaderAware需重写setResourceLoader
    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    @Override
    //实现BeanNameAware需重写setBeanName
    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public void outputResult(){
        System.out.println("Bean的名称为: " + beanName);
        Resource resource = loader.getResource("classpath:com/wisely/highlight_spring4/ch3/aware/test.txt");
        try {
            System.out.println("ResourceLoader加载的文件内容为: " + IOUtils.toString(resource.getInputStream()));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
